package com.example.hello;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity{
    private static final String TAG = "MainAcyivity";
   Float mybmi;
    String new_name;
    TextView badget,in_num;
    TextView name,bmi_num,basic_num,fat_num,co_num,sp_num;
    float basic = 0;
    float bodyfat = 0;
    float bmi = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        SharedPreferences sharedPreference1 = getSharedPreferences("food", Activity.MODE_PRIVATE);
//        Float number = sharedPreference1.getFloat("number",0.0f);
//        Float standard = sharedPreference1.getFloat("detail",0.0f);
//        String f_total = sharedPreference1.getString("f_total","");
//        SharedPreferences sharedPreference2 = getSharedPreferences("sport", Activity.MODE_PRIVATE);
//        Float sp_number = sharedPreference2.getFloat("sp_number",0.0f);
//        Float sp_standard = sharedPreference2.getFloat("sp_detail",0.0f);
//        String sp_total = sharedPreference2.getString("sp_total","");
        saved();
//        in_num = findViewById(R.id.in_num);
//        in_num.setText(f_total);
        badget = (TextView) findViewById(R.id.badget);
        DecimalFormat df=new DecimalFormat(".##");
        String bt=df.format(bmi);
        String bat=df.format(basic);
        String ft=df.format(bodyfat);
        bmi_num=findViewById(R.id.bmi_num);
        name=findViewById(R.id.user_name);
        basic_num=findViewById(R.id.basic_num);
        fat_num=findViewById(R.id.fat_num);
        name.setText("欢迎，"+new_name);
        bmi_num.setText(bt);
        basic_num.setText(bat);
        fat_num.setText(bt);
        badget.setText("推荐摄入"+bat+"cal");
        data_have();

    }
    private void saved() {
        Intent intent = getIntent();
        SharedPreferences sharedPreference = getSharedPreferences("message", Activity.MODE_PRIVATE);
        Float new_weight = sharedPreference.getFloat("weight_key",0.0f);
        Float new_age = sharedPreference.getFloat("age_key",0.0f);
        Float new_height = sharedPreference.getFloat("height_key",0.0f);
         new_name = sharedPreference.getString("name_key","");
        String new_sex = sharedPreference.getString("sex_key","");
        Log.i(TAG,"onCreate: new_weight=" + new_weight);
        Log.i(TAG,"onCreate: new_age=" +new_age);
        Log.i(TAG,"onCreate: new_height=" + new_height);
        Log.i(TAG,"onCreate: new_name=" + new_name);
        Log.i(TAG,"onCreate: new_sex=" + new_sex);
        //显示数据到控件
        bmi = new_weight / (new_height/100*new_height/100);
        if(new_sex =="female"){
            basic = 66.5f+13.8f*new_weight+5.0f*new_height-6.8f*new_age;
            bodyfat = (float) (1.2*bmi+0.23*new_age-5.4);
        }
        else{
            basic = 655f+9.6f*new_weight+1.8f*new_height-4.7f*new_age;
            bodyfat = (float) (1.2*bmi+0.23*new_age-5.4-10.8);
        }
        Log.i(TAG,"message: basic=" + basic);
        Log.i(TAG,"message: bodyfat=" +  bodyfat);
        Log.i(TAG,"message: bmi=" + bmi);
//将新设置的代谢率保存到sp里
        SharedPreferences sharedPreferences = getSharedPreferences("data", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat("basic",basic);
        //保存
        editor.commit();
        Log.i(TAG,"onActivityResult:数据已保存");

    }
    public void openFood(View btn) {
        Intent config = new Intent(this, foodlist.class);
        startActivity(config);//请求代码
        Log.i("open", "openSport: ");

    }
    public void openSport(View btn) {
        Intent config = new Intent(this, sportlist.class);
        startActivity(config);//请求代码
        Log.i("open", "openFood: ");

    }
    public void sp_list(View btn) {
        Intent config = new Intent(this,sport_message.class);
        startActivity(config);//请求代码
        Log.i("open", "open:sport_message: ");

    }

    public void list(View btn) {
        Intent config = new Intent(this,food_message.class);
        startActivity(config);//请求代码
        Log.i("open", "open:food_message: ");

    }

    public void data_have(){
    listManager manager = new listManager(this);
//       //设置头部容器中文本的显示
      float sum_all = manager.sum();
        float spsum_all = manager.sp_sum();
        co_num = findViewById(R.id.co_number);
        in_num = findViewById(R.id.in_num);
        sp_num = findViewById(R.id.sp_num);
        DecimalFormat df=new DecimalFormat(".##");
        String to=df.format(basic-sum_all+spsum_all);
        String in = df.format(sum_all);
        String sp = df.format(spsum_all);
        co_num.setText(to+"cal");
        in_num.setText(in+"cal");
        sp_num.setText(sp+"cal");
//      for(RateItem item : manager.listAll()){
//          co_num = findViewById(R.id.co_number);
//           if(item.getCurRate()!=null){
//          co_num.setText(String.valueOf(basic-Float.parseFloat(item.getCurRate())));
//              in_num = findViewById(R.id.in_num);
//              DecimalFormat df=new DecimalFormat(".##");
//              Float co_total=0+Float.parseFloat(item.getCurRate());
//              String to=df.format(co_total);
//               in_num.setText(to);
//          }
//
//       }
    }


}